package com.harmontronics.hanma.paas.paas.utils;

import com.harmontronics.hanma.paas.paas.exception.HttpErrorException;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.lang3.exception.ExceptionUtils;

import java.io.IOException;

@Slf4j
public class OkHttpUtils {

    public static String execNewCall(OkHttpClient client,Request request) throws IOException {
        Response response = null;
        try {
            response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                return response.body().string();
            }else{
                throw new HttpErrorException(response.code());
            }
        } finally {
            if (response != null) {
                response.close();
            }
        }
    }
}
